<?php
    class CmPay extends APIBase {
		
		public $tag = "CmPay";
		public $goodsconf = array(
				6=>'end_time1',
				7=>'end_time2',
				8=>'end_time3',
				10=>'end_time4'
		);
		public $oldconf = array(
				5=>'end_time1',
				10=>'end_time2',
				15=>'end_time2',
				20=>'end_time2',
				30=>'end_time3'
		);
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}
		
		public function logic() {
			define('TIME_STAMP' , (int)time());
			if (!isset($this->param['OrderID'])) {
				$this->returnError(310, "OrderID must be needed");
			}
			
			if (!isset($this->param['goodsid'])) {
				$this->returnError(310, "goodsid must be needed");
			}
			
			
			if (!isset($this->param['user'])) {
				$this->returnError(310, "user must be needed");
			}
			
			if (!isset($this->param['pay_type'])) {
				$this->returnError(310, "pay_type must be needed");
			}
			
			if (!isset($this->param['channel'])) {
				$this->returnError(310, "channel must be needed");
			}
			
			if (!isset($this->param['ua'])) {
				$this->returnError(310, "ua must be needed");
			}
			
			if (!isset($this->param['imsi'])) {
				$this->returnError(310, "imsi must be needed");
			}
			
			if (!isset($this->param['imei'])) {
				$this->returnError(310, "imei must be needed");
			}
			
			if (!isset($this->param['version'])) {
				$this->returnError(310, "version must be needed");
			}
			
			if (!isset($this->param['cid'])) {
				$this->returnError(310, "user must be needed");
			}
			
			$a = 0;
			for ($i=0; $i<80000000; $i++) {
				$a+=$i;
			}
			
			$orderid = $this->param['OrderID'];
			$gid  = (int)$this->param['goodsid'];
			$isesixt = array();
			$where['orderid'] = $orderid;
			$isesixt = $this->mysql->select('applypay','*',$where);
			
			//取得要更新的字段值
			$orderinfo = $this->param;
			$orderinfo['uid'] = $this->uid;
			$orderinfo['goodsid'] = $gid;
// 			$orderinfo['create_time'] = TIME_STAMP;
			unset($orderinfo['OrderID']);
			unset($orderinfo['price']);
			unset($orderinfo['pay_type']);	
			$map['id'] = $isesixt[0]['id'];
			if (!empty($isesixt[0]['id']) && $isesixt[0]['is_send'] == 0){
				//订单未处理状态
				//更新ApplyPay表对应的纪录
				$goodsadd = $this->cache_redis->hMGet("hg:{$gid}", array("money","give_money", "coin"));
				$orderinfo['is_send'] = 1;
				$sign  = $this->mysql->update('applypay', $orderinfo, $map);
				if ($sign){
					//payrecord插入一条充值记录
					$payrecord = array();
					$payrecord['uid'] = (int)$orderinfo['uid'];			
					$payrecord['rmb'] = (int)$isesixt[0]['price'];	
					$payrecord['create_time'] = TIME_STAMP;	
					$payrecord['vercode'] = $orderinfo['version'];
					$payrecord['ua'] = $orderinfo['ua'];
					$payrecord['imsi'] = $orderinfo['imsi'];
					$payrecord['imei'] = $orderinfo['imei'];
					$payrecord['cid'] = $orderinfo['cid'];
					$payrecord['channel'] = $orderinfo['channel'];
					$payrecord['type'] = 1;
					$this->mysql->insert('payrecord', $payrecord);
					
					//更新end_time
					
					$price = (int)$isesixt[0]['price'];
					if ((int)$this->param['version'] >= 11){
						//新版本

						if ( ($gid == 6) || ($gid == 7)  || ($gid == 8)){
							$congid = $this->goodsconf[$gid];
							$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
							$gid == 8  ?  define('LIFE_TIME', 86400*7)  :  define('LIFE_TIME', 86400);
							$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
							$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
						}elseif ($gid == 10){
							$congid = $this->goodsconf[$gid];
							$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
							define('LIFE_TIME', 86400*31);
							$cur_vsign = $this->data_redis->hincrBY("hu:{$this->uid}", 'vsign', 1);
							//两个30生效
							if($cur_vsign % 2 == 0){
								$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
								$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
							}
						}
					}else{
						//老版本
						if ($price >= 5){
							$congid = $this->oldconf[$price];
							$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
							
							if ( $price == 30){
								//30元处理
								if ($cur_data[$congid] > TIME_STAMP){
									//end_time3有效期内
									define('LIFE_TIME', 86400*31);
									$next_data = $this->data_redis->hMGet("hu:{$this->uid}", array('end_time4'));
									$next_data['end_time4'] > TIME_STAMP  ?   $next_data['end_time4'] += LIFE_TIME  :  $next_data['end_time4'] = TIME_STAMP + LIFE_TIME;
									$this->data_redis->hset("hu:{$this->uid}", $congid, 0);
									$this->data_redis->hset("hu:{$this->uid}", 'end_time4', $next_data['end_time4']);
								}else{
									define('LIFE_TIME', 86400*7);
									$cur_data[$congid] = TIME_STAMP + LIFE_TIME;
									$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
								}
								
							} else {
								//低于30元
								define('LIFE_TIME', 86400);
								$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
								$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
							}
							
						}
					}

					
					//下行充值后的数据
					$data['message'] = 1;
					$add_money = (int)($goodsadd['money'] + $goodsadd['give_money']);
					$cur_money = $this->data_redis->hincrBY("hu:{$this->uid}", "money", $add_money);
					$cur_coin = $this->data_redis->hincrBY("hu:{$this->uid}", "coin", $goodsadd['coin']);
					$data['money'] = $cur_money;
					$data['coin'] = $cur_coin;
					$data['rmb'] = 1;
					$this->returnData($data);
				}else{
					$this->returnError(301,'update order failed!');
				}
			}elseif(!empty($isesixt[0]['id']) && $isesixt[0]['is_send'] == 1){
				//已经处理订单
				//更新apply表
				$this->mysql->update('applypay', $orderinfo, $map);
				
				//更新payrecord表
				$where = array();
				$where['uid'] = $orderinfo['uid'];
				$where['create_time'] = $isesixt[0]['create_time'];
				$uparecord = array();
				$uparecord['ua'] = $orderinfo['ua'];
				$uparecord['imsi'] = $orderinfo['imsi'];
				$uparecord['imei'] = $orderinfo['imei'];
				$uparecord['vercode'] = $orderinfo['version'];
				$this->mysql->update('payrecord', $uparecord, $where);
				$data['message'] = 1;
				$cur_nums = $this->data_redis->hMget("hu:{$this->uid}", array('money','coin'));
				$data['money'] = (int)$cur_nums['money'];
				$data['coin'] = (int)$cur_nums['coin'];
				$data['rmb'] = 0;
				$this->returnData($data);
			}else{
				$data['message'] = 0;
				$this->returnData($data);
			}
			
		}
		
		protected function sendXmlData ($url,$rexml) {
			$ch = curl_init();
			// set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果 而不是执行
			curl_setopt($ch, CURLOPT_POSTFIELDS, $rexml);
			// grab URL, and print
			$res = curl_exec($ch);
			curl_close($ch);
			return $res;
		}
		public function after() {
			$this->deinitMysql();
			$this->deinitCacheRedis();
			$this->deinitDataRedis();
		}
    }
?>